package cn.hs.service.impl;

import cn.hs.entity.*;
import cn.hs.mapper.AppointmentMapper;
import cn.hs.mapper.ConsultMapper;
import cn.hs.mapper.SpecialtyMapper;
import cn.hs.mapper.UserMapper;
import cn.hs.service.AppointmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @ClassName AppointmentServiceImpl
 * @Description TODO
 * @Author CnYuu
 * @Date 2024年10月24日 11:08
 * @Version 1.0
 */
@Service
public class AppointmentServiceImpl implements AppointmentService {
    @Autowired
    private AppointmentMapper appointmentMapper;
    @Autowired
    private SpecialtyMapper specialtyMapper;
    @Autowired
    private ConsultMapper consultMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public PageInfo selectAll(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Appointment> appointments = appointmentMapper.selectAll();
        //遍历文章集合
        for(Appointment appointment:appointments){
            Integer sp = appointment.getTypeId();
            Specialty specialty = specialtyMapper.selectByPrimaryKey((long)sp);
            Integer userId = appointment.getUserId();
            User user = userMapper.selectById(userId);
            Integer consultId = appointment.getConsultId();
            Consult consult = consultMapper.selectByPrimaryKey((long)consultId);
            appointment.setConsult(consult);
            appointment.setUser(user);
            appointment.setSpecialty(specialty);
        }
        PageInfo<Appointment> pageInfo = new PageInfo<>(appointments);
        return pageInfo;
    }

    @Override
    public Appointment selectById(Appointment appointment) {
        return appointmentMapper.selectById(appointment.getId());
    }

    @Override
    public int insert(Appointment appointment) {
        return appointmentMapper.insertAll(appointment);
    }

    @Override
    public int delete(Appointment appointment) {
        return appointmentMapper.deleteById(appointment.getId());
    }

    @Override
    public int update(Appointment appointment) {
        return appointmentMapper.updateStatus(appointment.getStatus(),appointment.getId());
    }
    @Override
    public int updateReply(Appointment appointment) {
        return appointmentMapper.updateReplyById(appointment.getReply(),appointment.getId());
    }

    @Override
    public List<Appointment> selectByConsultId(Integer consultId) {
        return appointmentMapper.selectByConsultId(consultId);
    }

    @Override
    public List<Appointment> selectByUserId(Integer userId) {
        List<Appointment> appointments = appointmentMapper.selectByUserId(userId);
        //遍历文章集合
        for(Appointment appointment:appointments){
            Integer sp = appointment.getTypeId();
            Specialty specialty = specialtyMapper.selectByPrimaryKey((long)sp);
            Integer userId1 = appointment.getUserId();
            User user = userMapper.selectById(userId1);
            Integer consultId = appointment.getConsultId();
            Consult consult = consultMapper.selectByPrimaryKey((long)consultId);
            appointment.setConsult(consult);
            appointment.setUser(user);
            appointment.setSpecialty(specialty);
        }
        return appointments;
    }
}
